package com.gujianjian.rabbitmq;

import cn.hutool.json.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@SpringBootApplication
@RestController
@Slf4j
public class RabbitmqServerApplication {

    @Autowired
    private AmqpTemplate amqpTemplate;
    @Autowired
    private AmqpAdmin amqpAdmin;
    public static void main(String[] args) {
        SpringApplication.run(RabbitmqServerApplication.class, args);
    }

    @GetMapping("/send")
    public void send() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        JSONObject data = new JSONObject();
        data.set("id",124567);
        data.set("createTime", new Date());
        data.set("name", "顾健健");
        String message = objectMapper.writeValueAsString(data);
        log.info("============已发送============");
        amqpTemplate.convertAndSend("queue30s", message);
    }


    @GetMapping("/send2")
    public void send2(String delay) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        JSONObject data = new JSONObject();
        data.set("id",1000000);
        data.set("createTime", new Date());
        data.set("name", "顾健健2");
        data.set("delay", delay);
        Object message = objectMapper.writeValueAsString(data);
        log.info("============已发送{}============",delay);

        // *********** 这里有个坑在 ***********
        // 如果delay是先比较大，比如说 20 * 1000
        // 然后delay比先前的小，比如说 10 * 1000
        // 然后delay还是比先前的小，比如说 5 * 1000
        // 这样的话，消费者收到的消息会按照最大的 20 * 1000  到了才会全部被消费
        // 为了避免这样造成的bug，先参考send2_1

        amqpTemplate.convertAndSend("queue2", message, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration(delay);
                return message;
            }
        });
    }

    @GetMapping("/send2_1")
    public void send2_1(String delay) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        JSONObject data = new JSONObject();
        data.set("id",1000000);
        data.set("createTime", new Date());
        data.set("name", "顾健健2");
        data.set("delay", delay);



        // 自定义过期时间队列，等消费者被消费后，自动删除
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange","exchange.dlx1");
        arguments.put("x-dead-letter-routing-key","key.dlx1") ;
        String queueName = "queue.delay." + delay;
        Queue queue = new Queue(queueName, true, false, false,arguments);
        amqpAdmin.declareQueue(queue);


        data.set("queueName", queueName);
        Object message = objectMapper.writeValueAsString(data);

        amqpTemplate.convertAndSend(queueName, message, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration(delay);
                return message;
            }
        });
        log.info("============已发送{}============",delay);
    }
}
